<?php
/**
 * Created by PhpStorm.
 * User: yuanbo
 * Date: 2018/7/2
 * Time: 上午12:15
 */

namespace app\admin\controller;

use app\admin\model\User;
use think\Controller;

class Club extends  Controller
{
    //*******俱乐部列表*********
    public function clubList()
    {
        $clublist = db('club')->select();
        $this->assign('clubList',$clublist);
        //俱乐部id
        return $this->fetch('Club/clubList');

    }
    //********修改俱乐部********
    public function updateClub()
    {
        //被修改的club_id
        $club_id = input('id');
        //查询club的信息
        $club = db('club')->where('club_id',$club_id)->find();
        $this->assign('club',$club);
        return $this->fetch();
    }
    //修改
    public function  update()
    {
        $club_id = input('club_id');//俱乐部id
        $club_name = input('club_name');//俱乐部名称
        $club_intro = input('club_intro');//俱乐部简介
        $contact_num = input('contact_num');//联系方式
        $address = input('address');//地址
        //文件上传
        $files = request()->file('lunbo');
        $posterfile = request()->file('poster');
        //修改的信息
        $data = ['club_name' => $club_name, 'club_intro' => $club_intro, 'contact_num' => $contact_num,'address'=>$address];

        if (count($files) == 0 && is_null($posterfile)) {
            $rs = db('club')->where('club_id', $club_id)->update($data);
            if ($rs) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败,无修改内容');
            }
        } else if (count($files) > 0 && is_null($posterfile)) {

            foreach ($files as $i => $file) {
                //移动到上传的目录里
                $info = $file->rule('uniqid')->move('../public/static/img/club/uploads/lunbo');
                if ($info) {
                    $data['sowing' . (++$i)] = 'uploads/lunbo/' . $info->getFilename();
                } else {
                    //上传失败获取错误信息
                    echo $file->getError();
                    $this->error('上传轮播图失败');
                }

            }
            //修改信息,更新数据库
            $rs = db('club')->where('club_id', $club_id)->update($data);
            if ($rs) {
                $this->success('修改成功且上传轮播图成功');
            } else {
                $this->error('修改失败');
            }


        } else if (count($files) == 0 && !is_null($posterfile)) {
            //只有海报图上传
            $info = $posterfile->rule('uniqid')->move('../public/static/img/club/uploads/poster');
            if ($info) {
                $data['club_poster'] = 'uploads/poster/' . $info->getFilename();
            } else {
                //上传失败获取错误信息
                echo $posterfile->getError();
                $this->error('上传海报失败');
            }
            //修改信息,更新数据库
            $rs = db('club')->where('club_id', $club_id)->update($data);
            if ($rs) {
                $this->success('修改成功且上传海报成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            //轮播图和海报都有上传
            foreach ($files as $i => $file) {
                //移动到上传的目录里
                $lunbo_info = $file->rule('uniqid')->move('../public/static/img/club/uploads/lunbo');
                if ($lunbo_info) {
                    $data['sowing' . (++$i)] = 'uploads/lunbo/' . $lunbo_info->getFilename();
                } else {
                    //上传失败获取错误信息
                    echo $file->getError();
                    $this->error('上传轮播图失败');
                }

            }
            $pos_info = $posterfile->rule('uniqid')->move('../public/static/img/club/uploads/poster');
            if ($pos_info) {
                $data['club_poster'] = 'uploads/poster/' . $pos_info->getFilename();
            } else {
                //上传失败获取错误信息
                echo $posterfile->getError();
                $this->error('上传海报失败');
            }
            //修改信息,更新数据库
            $rs = db('club')->where('club_id', $club_id)->update($data);
            if ($rs)
            {
                $this->success('修改成功且上传海报和轮播图成功');
            }
            else {
                $this->error('修改失败');
            }

            }
        }
    //********添加俱乐部*******
        public function addClub()
    {
        return $this->fetch('Club/addClub');
    }

    //添加
    public  function  add()
    {
        $club_name = input('club_name');//俱乐部名称
        $club_intro = input('club_intro');//俱乐部简介
        $contact_num = input('contact_num');//联系方式
        $address = input('address');//俱乐部地址
        $business_hour = input('business_hour');//营业时间
        //文件上传
        $files = request()->file('lunbo');//轮播图
        $posterfile = request()->file('poster'); //海报

        $creat_time = date("Y-m-d h:i:s");
        //添加的信息
        $data=['club_id'=>null,'club_name'=>$club_name,'club_intro'=>$club_intro,'contact_num'=>$contact_num,
            'address'=>$address,'business_hour'=>$business_hour,'create_time'=>$creat_time];

        if (count($files) == 4 &&!is_null($posterfile))
        {
            //轮播图和海报都有上传
            foreach ($files as $i => $file)
            {
                //移动到上传的目录里
                $lunbo_info = $file->rule('uniqid')->move('../public/static/img/club/uploads/lunbo');
                if ($lunbo_info) {
                    $data['sowing' . (++$i)] = 'uploads/lunbo/' . $lunbo_info->getFilename();
                } else {
                    //上传失败获取错误信息
                    echo $file->getError();
                    $this->error('上传轮播图失败');
                }

            }
            $pos_info = $posterfile->rule('uniqid')->move('../public/static/img/club/uploads/poster');
            if ($pos_info) {
                $data['club_poster'] = 'uploads/poster/' . $pos_info->getFilename();
            } else {
                //上传失败获取错误信息
                echo $posterfile->getError();
                $this->error('上传海报失败');
            }

            //添加信息,更新数据库
            $rs = db('club')->insert($data);
            if ($rs)
            {
                $this->success('添加成功且上传海报和轮播图成功');
            }
            else {
                $this->error('添加失败');
            }

        }
        else{

            $this->error('添加失败,未上传海报或轮播图');
        }




    }
    //********删除俱乐部***********
    public  function  delete()
    {
        $club_id = input('club_id');
        db('admin')->where('club_id',$club_id)->update(['club_id'=>0]);
        $rs = db('club')->where('club_id',$club_id)->delete();
        $json = [];
        if ($rs)
        {
            $json = ['success'=>1];
        }
        else
        {
            $json = ['success'=>0];
        }
        return json($json);
    }





}